<?php

namespace App\Admin\Controllers;

use App\Models\AfterInformation;
use App\Models\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\MessageBag;

class PhoneController extends Controller
{
    use HasResourceActions;

    //首页
    public function index(Content $content)
    {
        return Admin::content(function (Content $content) {
            $content->header('售后信息列表');
            $content->body($this->grid());
        });
    }

    //编辑
    public function edit($id, Content $content)
    {
        return $content->header('售后信息')->description('编辑')->body($this->form()->edit($id));
    }

    //编辑保存
    public function update($id)
    {
        return $this->form()->update($id);
    }

    //添加售后信息
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('创建售后信息');
            $content->body($this->form());
        });
    }


    //添加售后信息保存回调
    public function store()
    {
        return $this->form()->store();
    }


    protected function grid()
    {
        return Admin::grid(AfterInformation::class, function (Grid $grid) {
            $grid->column('id', 'ID')->sortable();
            $grid->column('imei', 'IMEI');
            $grid->column('phone_model', '手机型号');
            $grid->column('deadline', '售后截止时间');
            $grid->user()->name('服务商');
            $grid->column('create_at', '添加时间');



            $grid->disableRowSelector();

            $grid->actions(function ($actions) {
                $actions->disableView();
            });

            $grid->tools(function ($tools) {
                // 禁用批量删除按钮
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });

            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();

                // 在这里添加字段过滤器
                $filter->column(1/3, function ($filter) {
                    $filter->like('imei', 'IMEI');
                });
            });

            $grid->model()->orderBy('id', 'desc');

            //禁用导出按钮
            $grid->disableExport();
        });
    }

    //修改表单事项
    protected function form()
    {
        // 创建一个表单
        return Admin::form(AfterInformation::class, function (Form $form) {
            $form->text('imei', 'IMEI')->rules('required');
            $form->text('phone_model', '手机型号')->rules('required');
            $form->datetime('deadline', '售后截止时间')->format('YYYY-MM-DD')->rules('required');
            $form->select('provider_id', '售后商')->options(function ($ids) {
                return DB::table('users')->where('identity', '2')->orWhere('identity', '3')->pluck('name', 'id');
            })->rules('required');
            $form->hidden('create_at')->default(date('Y-m-d', time()));

            // 定义事件回调，当模型即将保存时会触发这个回调
            $form->saving(function (Form $form) {
                $imei = DB::table('after_information')->where('imei', $form->imei)
                    ->where('id', '<>',$form->model()->id)->first();
                if(!empty($imei)) {
                    $error = new MessageBag([
                        'title'   => 'IMEI重复',
                        'message' => 'IMEI码已存在，请校验后输入',
                    ]);

                    return back()->with(compact('error'));
                }
                if(strtotime($form->deadline) < time()) {
                    $error = new MessageBag([
                        'title'   => '截止时间错误',
                        'message' => '截止时间日期必须大于当前日期，请校验后选择',
                    ]);

                    return back()->with(compact('error'));
                }
            });
            $form->tools(function (Form\Tools $tools) {
                // 去掉`删除`按钮
                $tools->disableDelete();
            });
        });
    }
}
